Concise Guide to Databases by Peter Lake & Paul Crowther
Author:Peter Lake & Paul Crowther
Language: eng
Format: epub, pdf
Publisher: Springer London, London
6.6 Using MongoDB’s db.collection.mapReduce() Method
As we saw in Chap. 5, MongoDB provides a number of built-in methods in the shell. We used the db.collection.find() method, for example, to search for records.
Another shell method provided is the db.collection.mapReduce() method which is a wrapper around the mapReduce command. We need to write a little bit of code to make this work for us in our environment—in effect creating our own extension to the Map-Reduce process.
Start by re-opening the MongoDB database. Assuming you inserted the Airport data in Chap. 5 you can jump straight to the Airports Database and we will be working with the AllAirports collection. If you did not do this you need to go to the end of the MongoDB tutorial in Chap. 5. The screen dump below reminds us how to do this before starting to build the functions required.
You would probably also need to remind yourself of what the data looks like (again, see Chap. 5). Our first Map Reduce exercise will count the number of Airports each country has.
Remembering that MapReduce is actually a two stage process we need to create first the map function and then the reduce function. In this case the AirportCount_map function adds the number 1 to the output against each instance of every Country Code. The reduce function will use the 1 for its count. The “emit” line is where we define what to output. The reduce function will then add up all the 1s for each Country Code. Reduce takes the output from Map and creates an Array. The word “this” is used to refer to “the collection currently being operated on”. When we call this function you will see that the collection is part of the call.
We have now created the functions (Fig. 6.4) required and the next step is to actually call the functions, using the mapReduce method that MongoDB provides. We have to tell the method which collection we are using, and this is what our map function will use as “this”.
Fig. 6.4Creating the MapReduce functions
Download
Concise Guide to Databases by Peter Lake & Paul Crowther.pdf
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(8309)
Test-Driven Development with Java by Alan Mellor(6800)
Data Augmentation with Python by Duc Haba(6717)
Principles of Data Fabric by Sonia Mezzetta(6464)
Learn Blender Simulations the Right Way by Stephen Pearson(6369)
Microservices with Spring Boot 3 and Spring Cloud by Magnus Larsson(6236)
Hadoop in Practice by Alex Holmes(5965)
Jquery UI in Action : Master the concepts Of Jquery UI: A Step By Step Approach by ANMOL GOYAL(5814)
RPA Solution Architect's Handbook by Sachin Sahgal(5638)
Big Data Analysis with Python by Ivan Marin(5400)
The Infinite Retina by Robert Scoble Irena Cronin(5325)
Life 3.0: Being Human in the Age of Artificial Intelligence by Tegmark Max(5159)
Pretrain Vision and Large Language Models in Python by Emily Webber(4364)
Infrastructure as Code for Beginners by Russ McKendrick(4133)
Functional Programming in JavaScript by Mantyla Dan(4044)
The Age of Surveillance Capitalism by Shoshana Zuboff(3964)
WordPress Plugin Development Cookbook by Yannick Lefebvre(3845)
Embracing Microservices Design by Ovais Mehboob Ahmed Khan Nabil Siddiqui and Timothy Oleson(3648)
Applied Machine Learning for Healthcare and Life Sciences Using AWS by Ujjwal Ratan(3622)
